﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AutoDataUpdateTool
{
    public partial class AutoOfficeTool : Form
    {
        Dictionary<string, string> RouteInfos;
        public AutoOfficeTool()
        {
            RouteInfos = new Dictionary<string, string>();
            ReadRouteFile();
            InitializeComponent();
        }
        public void ReadRouteFile()
        {
            using (var streamReader = File.OpenText(@"RouteInfo.Conf")) 
            {
                var lines = streamReader.ReadToEnd().Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                foreach (var line in lines)
                {
                    string[] infos = line.Split(',');
                    RouteInfos[infos[0]] = infos[1];
                }
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void OpenFileButton_Click(object sender, EventArgs e)
        {
            string RouteID = "000000";           
            var ofd = new OpenFileDialog
            {
                Multiselect = true
            };
            if(ofd.ShowDialog() == DialogResult.OK)
            {
                foreach (string _fileName in ofd.FileNames)
                {
                   
                    string RouteName = _fileName.Substring(_fileName.LastIndexOf('\\') +1 , _fileName.Length - 5 - _fileName.LastIndexOf('\\'));
                    if (RouteInfos.TryGetValue(RouteName, out RouteID))
                    {
                        RouteNumberTxtBox.Text = RouteID;
                        DataParse dps = new DataParse(_fileName);
                        DataSender datS = new DataSender(DataParse.BusRouteInfoList, RouteInfos[RouteName]);
                        SqlLineTxtBox.Text = "SQL line : " + datS.SqlLineRes;
                        //MessageBox.Show("Data has been updated to database!");
                        //Send2DatabaseButton.Enabled = false;
                    }
                }
            }
        }
        private void Send2DatabaseButton_Click(object sender, EventArgs e)
        {
            DataSender datS = new DataSender(DataParse.BusRouteInfoList,RouteNumberTxtBox.Text);
            SqlLineTxtBox.Text = "SQL line : " + datS.SqlLineRes;
            MessageBox.Show("Data has been updated to database!");
        }
    }
}
